Skip to content

fix(VVirtualScroll): preserve measured heights when items change to prevent scroll jump - #23066

Open
waterWang wants to merge 1 commit into
vuetifyjs:masterfrom
waterWang:fix/virtual-scroll-preserve-sizes-on-items-change
Open

fix(VVirtualScroll): preserve measured heights when items change to prevent scroll jump#23066
waterWang wants to merge 1 commit into
vuetifyjs:masterfrom
waterWang:fix/virtual-scroll-preserve-sizes-on-items-change

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

Description

When the items array changes (e.g. an item is removed via splice), the VVirtualScroll component completely resets its sizes and offsets arrays. This causes all previously measured heights to be lost, and offsets are recalculated using the default 16px estimate — which is far from actual measured heights — causing a visible scroll position jump.

Root Cause

In packages/vuetify/src/composables/virtual.ts, the watch(items, ...) handler:

watch(items, () => {
    sizes = Array.from({ length: items.value.length })  // All null!
    offsets = Array.from({ length: items.value.length }) // All 0!
    updateOffsets.immediate()
    calculateVisibleItems()
}, { deep: 1 })

Every item change discards all measured heights, forcing getSize() to fall back to the default itemHeight (16px). If actual items are 50px tall, offsets[500] drops from 25000 to 8000, causing paddingTop and the scroll position to jump.

Fix

Preserve existing height measurements for items that remain at the same index after the items array changes:

watch(items, (newItems, oldItems) => {
    const oldSizes = sizes.slice()
    sizes = Array.from({ length: newLength })
    offsets = Array.from({ length: newLength })

    // Preserve existing height measurements for items that remain
    // at the same index
    const minLength = Math.min(oldLength, newLength)
    for (let i = 0; i < minLength; i++) {
      if (oldSizes[i]) {
        sizes[i] = oldSizes[i]
      }
    }

    updateOffsets.immediate()
    calculateVisibleItems()
}, { deep: 1 })

This keeps offsets largely accurate when items are added or removed, preventing the scroll position from jumping.

Closes #22610

…revent scroll jump

When the items array changes (e.g. an item is removed via splice),
the sizes and offsets arrays were completely reset, causing all
previously measured heights to be lost. The offsets would then be
recalculated using the default estimate (16px), which is far from
the actual measured heights, causing a visible scroll position jump.

Fix: preserve existing height measurements for items that remain at
the same index after the items array changes. This keeps the offsets
largely accurate and prevents the scroll position from jumping.

Closes vuetifyjs#22610
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug Report][3.11.5] v-virtual-scroll - The page jumps after removing items from the list

1 participant